package com.dense.kuiniu.shelfcard.business;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dense.kuiniu.shelfcard.R;
import com.dense.kuiniu.shelfcard.base.BaseFragment;
import com.dense.kuiniu.shelfcard.base.Url;
import com.dense.kuiniu.shelfcard.card.idcard.IDCardInfo;
import com.rxjava.rxlife.RxLife;

import java.util.Objects;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.OnClick;
import rxhttp.RxHttp;

/**
 * @author Administrator
 * @date 2020/9/23
 */
public class LostCardDelegate extends BaseFragment {
    private static final String KEY = "info";

    public static LostCardDelegate getInstance(IDCardInfo info) {
        Bundle bundle = new Bundle();
        bundle.putParcelable(KEY, info);
        LostCardDelegate delegate = new LostCardDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R.id.tv_commit)
    TextView mTvCommit;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;
    @BindView(R.id.tv_msg)
    TextView mTvMsg;
    @BindView(R.id.iv_lost_card)
    ImageView mIvCard;

    private IDCardInfo mIDCardInfo;

    private State mState = State.Lost;

    private enum State {
        Lost, Loading, LostSucceed, Error
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mIDCardInfo = bundle.getParcelable(KEY);
        }
    }


    @OnClick({R.id.tv_cancel, R.id.tv_commit})
    public void onClick(View view) {
        super.onClick(view);
    }

    @Override
    protected void onClick2(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_commit:
                submit();
                break;
            case R.id.tv_cancel:
                pop();
                break;
        }
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.delegate_lost_card;
    }

    @Override
    protected void init() {
        if ("挂失".equals(mIDCardInfo.getCardState())) {
            mState = State.LostSucceed;
            showLostSucceed();
        }
    }

    private void submit() {
        switch (mState) {
            case Lost:
                submitLost();
                break;
            case LostSucceed:
                OutputCardDelegate delegate = findFragment(OutputCardDelegate.class);
                if (delegate == null) {
                    delegate = OutputCardDelegate.getInstance(mIDCardInfo);
                }
                startWithPop(delegate);
                break;
            case Error:
                pop();
                break;
        }
    }

    private void submitLost() {
        showLoading();
        RxHttp.get(Url.lostCard)
                .add("idCard", mIDCardInfo.getCardNo())
                .asResponse(String.class)
                .to(RxLife.toMain(this))  //感知生命周期，并在主线程回调
                .subscribe(str -> {
                    mState = State.LostSucceed;
                    showLostSucceed();
                }, throwable -> {
                    mState = State.Error;
                    showError(throwable.getMessage());
                });
    }


    public void showLoading() {
        mTvMsg.setText(R.string.text_commit_ing);
        mTvCommit.setVisibility(View.GONE);
        mTvCancel.setVisibility(View.GONE);
    }

    public void showError(String msg) {
        mTvMsg.setText(msg);
        mTvMsg.setTextColor(Objects.requireNonNull(getActivity()).getResources().getColor(R.color.colorFailed));
        mIvCard.setBackgroundResource(R.mipmap.ic_error);
        mTvCommit.setVisibility(View.VISIBLE);
        mTvCommit.setBackgroundResource(R.drawable.shape_bg_exit);
        mTvCancel.setVisibility(View.GONE);
    }


    public void showLostSucceed() {
        mTvMsg.setText(R.string.text_lost_succeed);
        mTvMsg.setTextColor(Objects.requireNonNull(getActivity()).getResources().getColor(R.color.font_black));
        mIvCard.setBackgroundResource(R.mipmap.ic_succeed);
        mTvCommit.setVisibility(View.VISIBLE);
        mTvCommit.setBackgroundResource(R.drawable.shape_bg_button_blue);
        mTvCancel.setVisibility(View.VISIBLE);
    }


}